<style lang="less">
@import "../../../assets/style/xinzheng.less";
</style>

<template>
	<mxq-router-layout>
		<div class="xinzheng">
			<Head slot="header" @headerClick="headerClick($event)">
				<div slot="headerTitle">
					<span>新增缺陷</span>
				</div>
				<div slot="headerRight">
					<i class="iconfont icon-shouye"/>
					<!-- <van-icon class="home" name="home"/> -->
				</div>
			</Head>
			<!--缺陷填写部分-->
			<qx-secete
				@getQuexianData="getQuexianData($event)"
				@upLoad="upLoad">
				<!--缺陷照片 start-->
				<div slot="quexian_photo_slot">
					<van-cell-group class="mt10">
						<van-field
							v-model="defectPhoto"
							class="detail_item"
							clearable
							label="缺陷照片"
							required
							disabled>
							<van-icon slot="icon" class="slot-cont">
								<p class="photo_holder">请拍摄缺陷照片</p>
								<!-- 拍照组件 -->
								<take-photo
									:photoLists="photoLists1"
									@getPhotosOne="getPhotosOne"
									@newArr="newArrOne"/>
							</van-icon>
						</van-field>
					</van-cell-group>
				</div>
				<!--缺陷照片 end-->

				<!-- 是否处理照片 start-->
				<div slot="chuli_photo_slot">
					<van-cell-group class="mt10">
						<van-switch-cell 
							v-model="isHandle" 
							title="是否处理" 
							size="20px" 
							@change="changeSwitch"/>
						<van-field
							class="detail_item"
							v-show="isShow">
							<van-icon slot="icon" class="slot-cont">
								<p class="photo_holder clearfix">请拍摄处理照片</p>
								<!-- 拍照 start -->
								<take-photo
									:photoLists="photoLists2"
									@getPhotosTwo="getPhotosTwo"
									@newArr="newArrTwo"/>
								<!-- 拍照 end -->
							</van-icon>
						</van-field>
					</van-cell-group>
				</div>
			</qx-secete>
			<!-- 是否处理照片 end-->
		</div>
	</mxq-router-layout>
</template>

<script>
import Head from '@/components/head'
import qxSecete from '../../components/quexian/add_quexian.vue'
import TakePhoto from '../../components/quexian-photo.vue'
import { fileData,callData } from '../../../axios/axios'
// import { openCamera, getLocation } from '../../../api/android'

import {
    Cell,
    CellGroup,
    Field,
    Button,
    Icon,
	SwitchCell,
	Toast,
	Dialog,
    Area,
    Picker
} from 'vant'

export default {
    name: 'quexianXingZheng',
    components: {
        Head,
        TakePhoto,
        qxSecete,
        [Cell.name]:Cell,
        [CellGroup.name]: CellGroup,
        [Field.name]: Field,
        [Button.name]: Button,
        [Dialog.name]:Dialog,
        [Icon.name]: Icon,
        [SwitchCell.name]: SwitchCell
    },
    data() {
        return {
            defectPhoto: null,		    	//缺陷照片
            isHandle: false,				//是否处理
            isShow: false,               	//是否处理拍照显示/隐藏
            defectsUpload: '',          	//新增缺陷上传
            cmDefectReviewId: '',       	//缺陷复核表ID 
            defectPhotoFileList:[],     	//缺陷处理-图片文件
            processingPhotoFileList: [],	//是否处理-图片文件
			photoNewArrOne: [],				//缺陷图片-删除图片后的新图片数组
			photoNewArrTwo: [],				//是否处理-删除图片后的新图片数组			
			photoLists1: [
                {
                    title: "缺陷照片",
                    type: 'getPhotosOne',
                    pictureType: 1,
                    photo:[]
                }
            ],
            photoLists2: [
                {
                    title: "是否处理拍照",
                    type: 'getPhotosTwo',
                    pictureType: 0,
                    photo:[]
                }
            ],
            lon:'',						//径度
            lat:'',						//纬度
            getListData:{}				// 填写缺陷信息返回的数据
        }
    },
    methods: {
        // 头部两侧按钮点击事件
		headerClick(e){
			this.$router.push({name:"index"})
		},
        // 缺陷照片-拍照回调
		getPhotosOne(params){
			this.defectPhotoFileList = params
		},
		// 是否处理-拍照回调
		getPhotosTwo(params){
			this.processingPhotoFileList = params
		},
		// 缺陷照片-删除
		newArrOne(params){
			this.photoNewArrOne = params
			console.log('接收到的newArrOne', this.photoNewArrOne);
		},
		// 是否处理项照片-删除
		newArrTwo(params){
			this.photoNewArrTwo = params
			console.log('接收到的newArrTwo', this.photoNewArrTwo);
		},
		// 是否处理选择
		changeSwitch(e){
            this.isShow=e
		},
		// 填写缺陷信息返回的数据
		getQuexianData(e){
			this.getListData = e
			console.log(e);
		},
		// 点击确定按钮
		upLoad(getListData) {
			var getListData = this.getListData
			let imgData1 = this.defectPhotoFileList
			let imgData2 = this.processingPhotoFileList
			console.log('imgData1-length=>', imgData1.file.length);
			console.log('photoNewArrOne',this.photoNewArrOne.length);
			console.log('photoNewArrTwo',this.photoNewArrTwo.length);
			console.log('imgData2-length',imgData2.file.length);
			
			var defectNatureJoin = this.$route.query.name + '-' + getListData.defectNature;//缺陷定性拼接
			//限制必须拍照
			if((imgData1.file && imgData1.file.length == 0 && this.photoNewArrOne.length == 0) || (imgData2.file && imgData2.file.length ==0 && this.photoNewArrTwo.length == 0 && this.isHandle == true)){
				Dialog.alert({
					title: '提示',
					message: '请拍照！'
				}).then(() => {
					
				});
				return
			}
			/* 
			if(window.Cordova){	//android 传缺陷地点的经纬度
				//限制必须拍照
				if((imgData1.file && imgData1.file.length == 0) || (imgData2.file && imgData2.file.length ==0 && this.isHandle == true)){
					Dialog.alert({
						title: '提示',
						message: '请拍照！'
					}).then(() => {
						
					});
					return
				}
				getLocation(res => {
                     let lon = res.data.lon;    //经度
					 let lat = res.data.lat;    //纬度
					 this.lon = lon;//经度
                     this.lat= lat;//纬度
	    			})
				params.jd = this.lon;    //经度
				params.wd = this.lat;    //纬度 
			}
			*/
			var params = {};
			params.jd = imgData1.jdwd[0].lon     //经度
			params.wd = imgData1.jdwd[0].lat     //纬度
			params.defectContent = getListData.defectInfo                 //缺陷内容
			params.defectClassification = getListData.defectLevel         //缺陷级别
			params.causeAnalysis = getListData.defectReason               //原因分析
			params.characterizationId = getListData.childNodeId           //缺陷定性叶子节点ID
			params.defectStatus = 0                                //页面状态 0新增页面，1复核和消缺页面
			params.defectCharacterization = defectNatureJoin      //缺陷定性
			params.lineId = this.$route.query.lineId               //线路ID
			params.lineTowerId = this.$route.query.lineTowerId     //杆塔线路ID
			params.towerId = this.$route.query.towerId             //物理杆塔ID
			params.taskId = this.$route.query.taskId               //任务ID
			params.taskType = this.$route.query.index              //任务类型: 0常规巡视 1特殊巡视 2保电值守 3线路检修
			params.towerTaskId = this.$route.query.towerTaskId   //杆塔任务ID
			params.defectType = this.$route.query.defectType       //缺陷类型
			//审核类型reviewType ''新增，0复核，1消除
			if(this.isHandle == false) params.reviewType = '';
			if(this.isHandle == true)  params.reviewType = 1;
			callData('/accountServer/CmDefect/addOrReviewDefect', 'post', params).then(res => {
				console.log('保电接口参数params',params);
				console.log('params.jd',params.jd);
				console.log('params.wd',params.wd);
				if(res.success){
					this.defectsUpload = res.data.cmDefectId;           //缺陷ID
					this.cmDefectReviewId = res.data.cmDefectReviewId; //缺陷复核表ID
					// 缺陷照片-遍历上传
					for(var i = 0; i<imgData1.file.length;i++){
						if(window.Cordova){
							let filePath = imgData1.file[i]      //图片信息
							let params2 = {}
								params2.taskId = this.$route.query.taskId                //任务ID
								params2.towerTaskId = this.$route.query.towerTaskId    	 //杆塔任务ID
								params2.towerId = this.$route.query.towerId              //物理杆塔ID
								params2.lineTowerId = this.$route.query.lineTowerId      //杆塔线路ID
								params2.jd = imgData1.jdwd[i].lon                           //经度
								params2.wd = imgData1.jdwd[i].lat                         	 //纬度
								params2.pictureType =  imgData1.pictureType
								params2.defectReviewId = this.cmDefectReviewId           //缺陷复核表ID
								params2.defectId = this.defectsUpload                    //缺陷ID
							fileData('/fileserver/CmDefectPicture/saveCmDefectPicture', 'post',params2,filePath).then(res => {
								if(res.success){
									Toast(res.data);
								}
							})
						}else{
							let params2 = new FormData()
								params2.append("taskId",this.$route.query.taskId)                //任务ID
								params2.append("towerTaskId",this.$route.query.towerTaskId)    	 //杆塔任务ID
								params2.append("towerId",this.$route.query.towerId)              //物理杆塔ID
								params2.append("lineTowerId",this.$route.query.lineTowerId)      //杆塔线路ID
								params2.append("jd",imgData1.jdwd[i].lon)                        //经度
								params2.append('wd',imgData1.jdwd[i].lat)                        //纬度
								params2.append('pictureType',imgData1.pictureType)
								params2.append('defectReviewId',this.cmDefectReviewId)           //缺陷复核表ID
								params2.append('defectId',this.defectsUpload)                    //缺陷ID
								params2.append("file",imgData1.file[i])      		 			 //图片信息
							fileData('/fileserver/CmDefectPicture/saveCmDefectPicture', 'post',params2).then(res => {
								if(res.success){
									Toast(res.data);
								}
							})
						}
					}
					// 是否处理-遍历上传
					for(var j = 0; j<imgData2.file.length;j++){
						if(window.Cordova){
							let filePath = imgData2.file[j]      //图片信息
							let params3 = {}
								params3.taskId = this.$route.query.taskId                //任务ID
								params3.towerTaskId = this.$route.query.towerTaskId      //杆塔任务ID
								params3.towerId = this.$route.query.towerId              //物理杆塔ID
								params3.lineTowerId = this.$route.query.lineTowerId      //杆塔线路ID
								params3.jd = imgData2.jdwd[j].lon                           //经度
								params3.wd = imgData2.jdwd[j].lat                         	 //纬度
								params3.pictureType =  imgData2.pictureType
								params3.defectReviewId = this.cmDefectReviewId           //缺陷复核表ID
								params3.defectId = this.defectsUpload                    //缺陷ID
							fileData('/fileserver/CmDefectPicture/saveCmDefectPicture', 'post',params3,filePath).then(res => {
								if(res.success){
									Toast(res.data);
								}
							})
						}else{
							let params3 = new FormData();
								params3.append("taskId",this.$route.query.taskId)                //任务ID
								params3.append("towerTaskId",this.$route.query.towerTaskId)      //杆塔任务ID
								params3.append("towerId",this.$route.query.towerId)              //物理杆塔ID
								params3.append("lineTowerId",this.$route.query.lineTowerId)      //杆塔线路ID
								params3.append("jd",imgData2.jdwd[j].lon )                                 //经度
								params3.append('wd',imgData2.jdwd[j].lat)                         //纬度
								params3.append('pictureType',imgData2.pictureType)
								params3.append('defectReviewId',this.cmDefectReviewId)           //缺陷复核表ID
								params3.append('defectId',this.defectsUpload)                    //缺陷ID
								params3.append("file",imgData2.file[j])			 				//图片信息
							fileData('/fileserver/CmDefectPicture/saveCmDefectPicture', 'post',params3).then(res => {
								if(res.success){
									Toast(res.data);
								}
							})
						}
					}
					// 返回上一页
					this.$router.go(-1)
				}
			})
		}
    },
    mounted() {
    }
}
</script>